package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.dao.AdvertisementDao;
import com.fuhe.chen.vendingmachine.dto.cond.AdvertisementCond;
import com.fuhe.chen.vendingmachine.pojo.Advertisement;
import com.fuhe.chen.vendingmachine.pojo.Log;
import com.fuhe.chen.vendingmachine.service.IAdvertisementService;
import com.fuhe.chen.vendingmachine.service.ILogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdvertisementServiceIpml implements IAdvertisementService {

    @Autowired
    AdvertisementDao advertisementDao;

    @Autowired
    ILogService logService;

    @Override
    public void addAdvertisement(Advertisement advertisement) {
        advertisementDao.addAdvertisement(advertisement);
        logService.addLog(Log.logging("添加广告信息"));
    }

    @Override
    public void deleteAdvertisement(Integer advertisementId) {
        advertisementDao.deleteAdvertisement(advertisementId);
        logService.addLog(Log.logging("删除广告信息"));
    }

    @Override
    public void updateAdvertisement(Advertisement advertisement) {
        advertisementDao.updateAdvertisement(advertisement);
        logService.addLog(Log.logging("修改广告信息"));
    }

    @Override
    public Advertisement findOne(Integer advertisementId) {
        return advertisementDao.findOne(advertisementId);
    }

    @Override
    public PageInfo<Advertisement> findByCondition(AdvertisementCond advertisementCond,Integer page,Integer num) {
        PageHelper.startPage(page,num);
        List<Advertisement> advertisements= advertisementDao.findByCondition(advertisementCond);
        PageInfo<Advertisement> advertisementPageInfo = new PageInfo<>(advertisements);
        return advertisementPageInfo;
    }

    @Override
    public PageInfo<Advertisement> findAll(int page, int num) {
        PageHelper.startPage(page,num);
        List<Advertisement> advertisements = advertisementDao.findAll();
        PageInfo<Advertisement> advertisementPageInfo = new PageInfo<>(advertisements);
        return advertisementPageInfo;
    }
}
